package bin_tree.heap;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

public class PriorityQueueTest {
    public static void main(String[] args) {
        // 通过构造方法传入比较器
//        Queue<Student> queue = new PriorityQueue<>(new StudentCom());
//        Queue<Student> queue = new PriorityQueue<>(new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o2.getAge() - o1.getAge();
//            }
//        });
        Queue<Student> queue = new PriorityQueue<>(((o1, o2) -> o2.getAge() - o1.getAge()));
        Student stu1 = new Student("李雷", 25);
        Student stu2 = new Student("韩梅梅", 20);
        Student stu3 = new Student("小明", 40);
        queue.offer(stu1);
        queue.offer(stu2);
        queue.offer(stu3);
        System.out.println(queue);
    }
}

//升序比较
class StudentCom implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
    }
}

//降序比较
class StudentComDesc implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o2.getAge() - o1.getAge();
    }
}

class Student {
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    //将当前对象和传入的对象比较
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (this == o) {
            return true;
        }
        //传入的o是否是Student类中new出来的
        if (!(o instanceof Student)) {
            //不是同一类型，没有可比性
            return false;
        }
        //向下转型还原为Studen类
        Student stu = (Student) o;
        return this.age == stu.age && this.name.equals(stu.name);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //@Override
//    public int compareTo(Student o) {
//        return this.age - o.age;
//    }
}
